package controllers.customer;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.AvailableItemService;
import services.CommentService;
import services.PurchasedItemService;
import domain.AvailableItem;
import domain.Comment;
import domain.PurchasedItem;
import forms.ItemForm;

@Controller
@RequestMapping("/purchasedItem/customer")
public class PurchasedItemCustomerController {

	// Services
	@Autowired
	private PurchasedItemService purchasedItemService;
	@Autowired
	private AvailableItemService availableItemService;
	@Autowired
	private CommentService commentService;

	// Constructors

	public PurchasedItemCustomerController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<PurchasedItem> purchasedItems;

		purchasedItems = purchasedItemService.findPurchasedItemsBySellerId();

		result = new ModelAndView("purchasedItem/list");
		result.addObject("requestURI", "purchasedItem/customer/list.do");
		result.addObject("purchasedItems", purchasedItems);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	// Listing methods
	@RequestMapping(value = "/buyerList", method = RequestMethod.GET)
	public ModelAndView purchasedItemList() {
		ModelAndView result;
		Collection<PurchasedItem> purchasedItems;

		purchasedItems = purchasedItemService.findPurchasedItemsByBuyerId();

		result = new ModelAndView("purchasedItem/list");
		result.addObject("requestURI", "purchasedItem/customer/buyerList.do");
		result.addObject("purchasedItems", purchasedItems);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	// Edition

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int purchasedItemId) {
		ModelAndView result;

		int availableItemId;
		PurchasedItem purchasedItem;
		AvailableItem availableItem;
		ItemForm itemForm;
		Collection<Comment> comments;
		Boolean isBuyer;

		purchasedItem = purchasedItemService.findOne(purchasedItemId);
		itemForm = purchasedItemService.purchasedItemToForm(purchasedItem);

		availableItemId = availableItemService
				.findAvailableItemIdByPurchasedItemRefNumber(itemForm
						.getReferenceNumber());

		availableItem = availableItemService.findOne(availableItemId);
		isBuyer = purchasedItemService.isBuyer(availableItem);
		comments = commentService.findCommentsByItemId(availableItemId);

		result = new ModelAndView("purchasedItem/edit");

		result.addObject("requestURI", "purchasedItem/customer/edit.do");
		result.addObject("itemForm", itemForm);
		result.addObject("availableItemId", availableItemId);
		result.addObject("comments", comments);
		result.addObject("isBuyer", isBuyer);
		result.addObject("purchasedItemId", purchasedItemId);
		result.addObject("backURI", "/welcome/index.do");

		return result;

	}

}
